import re

word_list = {}
# 存取
with open("Walden.txt","r",encoding='utf-8') as f:
    file = f.read()

# 处理文本，统计单词频率
for i in '·!"#$%&()*+-,-./:;<=>?@“”[\\]^_{|}~':
    file = file.replace(i," ")
file = file.lower()
words = file.split()
for i in words:
    word_list[i] = word_list.get(i, 0)+1

# 排序
word_list = sorted(word_list.items(),key= lambda x:x[1],reverse=True)

# 输出
tplt = "{0:^8}\t{1:^8}"
print(tplt.format("单词","频率"))
for i in range(10):
    print(tplt.format(word_list[i][0],word_list[i][1]))
